// JSON转excel：
// 第一个为excel第一列，第二个为json文件，第三保存文件名，第四为是否第一类沈序号
function excel(firstRow,JSONData, FileName,number=false) {
    if(!JSONData){
        return;
    }
    var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
    var excel = "<table>";
    // 生成头部 firstRow
    row = "<tr>";
    if(number){
        row+= "<td align='center'>序号</td>";
        for (let i = 0; i < firstRow.length; i++) {
            row+= "<td align='center'>" +firstRow[i]+  "</td>";
        }
    }else{
        for (let i = 0; i < firstRow.length; i++) {
            row+= "<td align='center'>"+firstRow[i]+  "</td>";
        }
    }
    row +="</tr>";
    //下部 每一行显示一个数据
    // 上部显示数据，每一行显示
    if(number){
        for (let j = 0; j < arrData.length; j++) {
            row += "<tr>";
            row += "<td align='center'>" + (j+1) + "</td>";
            for (let k = 0; k < arrData[j].length; k++) {
                row += "<td align='center'>" +  arrData[j][k] + "</td>";
            }
            row +="</tr>";
        }
    }else{
        for (let j = 0; j < arrData.length; j++) {
            row += "<tr>";
            for (let k = 0; k < arrData[j].length; k++) {
                row += "<td align='center'>" +  arrData[j][k] + "</td>";
            }
            row +="</tr>";
        }
    }

    //row添加
    excel+=row
    excel += "</table>";
    //截止

    var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
    excelFile += '; charset=UTF-8">';
    excelFile += "<head>";
    excelFile += "<!--[if gte mso 9]>";
    excelFile += "<xml>";
    excelFile += "<x:ExcelWorkbook>";
    excelFile += "<x:ExcelWorksheets>";
    excelFile += "<x:ExcelWorksheet>";
    excelFile += "<x:Name>";
    excelFile += "{worksheet}";
    excelFile += "</x:Name>";
    excelFile += "<x:WorksheetOptions>";
    excelFile += "<x:DisplayGridlines/>";
    excelFile += "</x:WorksheetOptions>";
    excelFile += "</x:ExcelWorksheet>";
    excelFile += "</x:ExcelWorksheets>";
    excelFile += "</x:ExcelWorkbook>";
    excelFile += "</xml>";
    excelFile += "<![endif]-->";
    excelFile += "</head>";
    excelFile += "<body>";
    excelFile += excel;
    excelFile += "</body>";
    excelFile += "</html>";
    var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
    var link = document.createElement("a");
    link.href = uri;
    link.style = "visibility:hidden";
    link.download = FileName + ".xls";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}
